package vn.bus.algorithms;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import com.google.gson.Gson;

import vn.bus.dto.LabelDTO;
import vn.bus.service.STPService;

@Path("/json/shortestpath/dep={departure}&arr={arrival}&fp={firstpriority}&sp={secondpriority}&tp="
		+ "{thirdpriority}&minwalk={minwalk}&maxwalk={maxwalk}&timedep={timedep}")
public class ShortestPath {
	
	@GET
	public String getInformationRequest(
			@PathParam("departure") String departure,
			@PathParam("arrival") String arrival,
			@PathParam("firstpriority") String firstpriority,
			@PathParam("secondpriority") String secondpriority,
			@PathParam("thirdpriority") String thirdpriority,
			@PathParam("minwalk") int minwalk,
			@PathParam("maxwalk") int maxwalk, @PathParam("timedep") int timedep) {

		// time start
		long startTime = System.nanoTime();
		
		ArrayList<String> priority = new ArrayList<String>();
		priority = setPriority(firstpriority, secondpriority, thirdpriority);
		// Shortest Path
		STPService stp = new STPService();
		ArrayList<LabelDTO> dt = stp.ShortestPath(departure, arrival, priority, minwalk, maxwalk,
				timedep);
		 Gson gson = new Gson();
		 String rs = gson.toJson(dt);

		// time end
		long stopTime = System.nanoTime();
		System.out.println("nanoTime = " + (stopTime - startTime));
		System.out.println("Time second = "
				+ TimeUnit.SECONDS.convert(stopTime - startTime,
						TimeUnit.NANOSECONDS));
		
		return rs;
	}
	
	public ArrayList<String> setPriority(String fp, String sp, String tp){
		ArrayList<String> pr = new ArrayList<String>();
		if(!fp.equals("none")){
			pr.add(fp);
		}
		if(!sp.equals("none")){
			pr.add(sp);
		}
		if(!tp.equals("none")){
			pr.add(tp);
		}
		if(fp.equals("none") && sp.equals("none") && tp.equals("none")){
			pr.add("st");	pr.add("sd");	pr.add("sp");
		}
		return pr;
	}
}
